/*******************************************************************************
 *
 *    Copyright (c) Dolphin Browser
 *
 *    ViewTest
 *
 *    ScrollableLinearLayout
 *    TODO File description or class description.
 *
 *    @author: derron
 *    @since:  2010-3-8
 *    @version: 1.0
 *
 ******************************************************************************/
package com.sdk.widget.threescreen.scrollview;

import com.sdk.widget.threescreen.scrollview.MotionDetector.MotionIntent;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;

public class ScrollableLinearLayout extends FlingableLinearLayout {

    Rect tmpRect = new Rect();
    private boolean mScrollable = true;
    public ScrollableLinearLayout(Context paramContext) {
        super(paramContext);
    }

    public ScrollableLinearLayout(Context paramContext,
            AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
    }

    @Override
    public boolean isScrollable() {
        return mScrollable;
    }

    public void setScrollable(boolean scrollable) {
        mScrollable = scrollable;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean result = false;
        if( ev.getPointerCount() > 1){
            return super.onInterceptTouchEvent(ev);
        }
        int action = ev.getAction();
        if(MotionEvent.ACTION_DOWN == action){
            if(!isScrollMode){
                stopScroll();
            }
            motionDetector.start(ev);
            motionIntent = MotionDetector.MotionIntent.UNKNOWN;

        }
        else if(MotionEvent.ACTION_MOVE== action){
            if(MotionDetector.MotionIntent.UNKNOWN == motionIntent){

                motionDetector.addMotionEvent(ev);
                motionIntent = motionDetector.detect();
                if(MotionDetector.MotionIntent.HORIZONTAL_SCROLL == motionIntent){
                    //当前的Page不是WebView时开始整个Page的滚动
                    if(1 != getCurrentPageIndex()){
                        result = true;
                        preX = (int) ev.getX();
                    }
                    else {
                        VelocityTracker localVelocityTracker = this.motionDetector.getVelocityTracker();
                        localVelocityTracker.computeCurrentVelocity(VELOCITY_MEASURE_TIME_MS);
                        int vx = (int) localVelocityTracker.getXVelocity();
                        int vy = (int) localVelocityTracker.getYVelocity();
                        int x = (int)ev.getX();
                        int y = (int)ev.getY();
//						ScrollPageView pageView = getTouchedPageView(x, y);
                        ScrollPageView pageView = getMiddlePageView();
                        if( mScrollable && null != pageView && pageView.shouldWorldScroll(x,y,vx,vy)){
                            preX = (int) ev.getX();
                            result = true;
                        }
                        else {
                            motionIntent = MotionIntent.UNKNOWN;
                        }
                    }
                }
            }
            else{
                result = false;
            }
        }
        else if (MotionEvent.ACTION_UP == action) {
            motionDetector.reset();
        }
        if(result) {
            //Log.v("Start scroll left/right");
        }
        return result;
    }

    @Override
    public void addView(View child, int index,
            android.view.ViewGroup.LayoutParams params) {
        if(child instanceof ScrollPageView){
            super.addView(child, index, params);
        }
        else {
            throw new RuntimeException("ScrollableLinearLayout can only contain ScrollPageView.");
        }
    }

    private ScrollPageView getTouchedPageView(int x,int y){
        Rect tmp = new Rect();
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            child.getGlobalVisibleRect(tmp);
            if(tmp.contains(x, y)){
                return (ScrollPageView) child;
            }
        }
        return null;
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean result = false;
        if(ev.getPointerCount() > 1){
            return super.onTouchEvent(ev);
        }
        if (MotionDetector.MotionIntent.HORIZONTAL_SCROLL == motionIntent) {
            int action = ev.getAction();

            if (MotionEvent.ACTION_MOVE == action) {
                scrollInTouch(ev);
                motionDetector.addMotionEvent(ev);
            } else if (MotionEvent.ACTION_UP == action) {
                motionDetector.reset();
            }
            result = true;
        }
        return result;
    }

    @Override
    public void scrollTo(int x, int y) {
        if (scrollable) {

            int measuredWidth = getMeasuredWidth();
            int width = getWidth();
            measuredWidth -= width;
            measuredWidth += rightScrollMargin;
            int i = leftScrollMargin;
            i = -i;
            if (x < i) {
                measuredWidth = -leftScrollMargin;
            } else {
                if (x <= measuredWidth) {
                    measuredWidth = x;
                }
            }

            int l_barWidth = getLeftPageView().getWidth();
            int r_barWidth = getRightPageView().getWidth();

            if (measuredWidth >= 0 && measuredWidth <= l_barWidth + r_barWidth) {
                super.scrollTo(measuredWidth, y);
            }

        }
    }

    public int getCurrentPageIndex(){
        if(getChildAt(0).getGlobalVisibleRect(tmpRect)){
            return 0;
        }
        else if(getChildAt(2).getGlobalVisibleRect(tmpRect)){
            return 2;
        }
        else {
            return 1;
        }
    }

    public ScrollPageView getLeftPageView(){
        return (ScrollPageView) getChildAt(0);
    }

    public ScrollPageView getMiddlePageView(){
        return (ScrollPageView) getChildAt(1);
    }

    public ScrollPageView getRightPageView(){
        return (ScrollPageView) getChildAt(2);
    }
}
